
/**
* @Title: ItemSearchDaoImpl.java
* @Package com.leetone.search.dao.impl
* @Description: 【】
* @date 2018年4月3日 下午11:22:33
* @version V1.0
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/

package com.leetone.search.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.leetone.search.dao.ItemSearchDao;
import com.leetone.search.pojo.Item;
import com.leetone.search.pojo.SearchResult;

/**
* @ClassName: ItemSearchDaoImpl
* @Description: 【】
* @date 2018年4月3日 下午11:22:33
* @Company: 上海
* @author:[谭磊][leetone123@163.com]
*/
@Repository("itemSearchDao")
public class ItemSearchDaoImpl implements ItemSearchDao {

	@Autowired
	private SolrServer solrServer;

	@Override
	public SearchResult searchItem(SolrQuery solrQuery) throws Exception {
		SearchResult result = new SearchResult();
		//根据查询条件创建查询
		QueryResponse response = solrServer.query(solrQuery);
		//取查询结果
		SolrDocumentList solrDocumentList = response.getResults();
		//获取总页数
		long total = solrDocumentList.getNumFound();
		result.setRecordCount(total);
		List<Item> itemList = new ArrayList<>();
		//取高亮显示的title
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		//取商品列表
		for(SolrDocument document:solrDocumentList)
		{
			Item item = new Item();
			item.setId((String) document.get("id"));
			//取高亮显示的title
			List<String> list = highlighting.get(document.get("id")).get("item_title");
			String title = "";
			if(!CollectionUtils.isEmpty(list))
			{
				title = list.get(0);
			}else {//如果没有高亮显示就直接获取原来的值
				title = (String) document.get("item_title");
			}
			item.setTitle(title);
			item.setCategoryName((String) document.get("item_category_name"));
			item.setImage((String) document.get("item_image"));
			item.setItemDes((String) document.get("item_desc"));
			item.setPrice((long) document.get("item_price"));
			item.setSellPoint((String) document.get("item_sell_point"));
			itemList.add(item);
		}
		result.setItemList(itemList);
		return result;
	}

}
